#ifndef ICONSTYLEITEMDELEGATE_H
#define ICONSTYLEITEMDELEGATE_H

#include <QCache>
#include <QStyledItemDelegate>

class IconStyleItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    IconStyleItemDelegate(QObject *parent = nullptr);
    ~IconStyleItemDelegate();

    void setViewVisibleRect(const QRect &rect);

    // 在视图滚动时，可以暂停不可见项的动画
    void setVisibleIndexes(const QModelIndexList &indexes);

    bool isActuallyGif(const QString &filePath) const;

    // QAbstractItemDelegate interface
public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void paint2(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const ;//const override;

    // QAbstractItemDelegate interface
public:
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;

signals:
    void requestUpdate(const QModelIndex &index);
    void rightClicked(const QModelIndex &index, const QPoint &globalPos);

private slots:
    void onFrameChanged(int frameNumber);

private:
    mutable QHash<QString, QMovie*> m_movies; // 存储GIF动画
    QPixmap m_placeholder; // 静态图片占位符
    QRect m_visibleRect; // 存储当前可见区域
    mutable QHash<QString, bool> m_fileTypeCache; // 缓存文件类型检测结果
    mutable QCache<QString, QPixmap> m_pixmapCache; // 缓存机制

    // QAbstractItemDelegate interface
public:
    virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
};

#endif // ICONSTYLEITEMDELEGATE_H
